<!-- 设备表格 -->

<template>
  <div>
    <WebTable :list="one.deviceList || []" noPage auto>
      <RowDict label="设备类型" bind="deviceCategoryId" :dict="$root.iot_category_selectDict" :width="200" />
      <RowDict label="设备类别" bind="deviceTypeId" :dict="$root.iot_type_selectDict" />
      <RowText :label="`${CORE.MAP.ASSET}名称`" bind="deviceName" :mixWidth="200" />
      <RowText :label="`${CORE.MAP.ASSET}编号`" bind="deviceCode" />
      <RowOperate :btns="{ 1: ['rej'], 2: ['pass'] }" pass="绑定" rej="解绑" :onPass="toOpen" :onRej="unbind" />
    </WebTable>

    <EasyModal v-model="easyVisible" title="绑定设备" medium @submit="toSubmit">
      <ChooseIOTOne
        :obj="obj"
        :deviceCategoryId="obj.deviceCategoryId"
        :deviceTypeId="obj.deviceTypeId"
        bind="device"
        disableType
      />
    </EasyModal>
  </div>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'DeviceTable',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb()],

  /**
   * 状态数据 (函数返回对象)
   */
  data() {
    return {
      easyVisible: false, // 弹窗是否可见
      obj: { // 数据对象
        device: {},
        deviceCategoryId: '',
        deviceTypeId: '',
        areaId: '',
      },
    }
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 解绑
     */
    async unbind(row) {
      await CORE.CONFIRM.warning('确定解绑该设备吗？', '解绑设备')

      this.one.device = row
      this.startRej()
    },

    /**
     * 弹窗
     */
    toOpen({ deviceCategoryId, deviceTypeId }) {
      const { areaId } = this.one
      this.obj = { device: {}, deviceCategoryId, deviceTypeId, areaId }
      this.easyVisible = true
    },

    /**
     * 提交
     */
    toSubmit() {
      if (CORE.isEmpty(this.obj.device)) {
        CORE.MSG.warning('请选择设备')
        return
      }

      const { id, ...other } = this.obj.device
      this.one.device = { ...other, deviceId: id, monitorPointSiteId: this.one.id }
      this.easyVisible = false
      this.startPass()
    },

    /**
     * 重置
     */
    toReset() {
      this.obj = { device: {} } // 清除选中
    },
  },
}
</script>
